package com.hopedove.tempdemo

import android.os.Bundle
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.bumptech.glide.Glide
import com.google.zxing.BarcodeFormat
import com.hopedove.tempdemo.databinding.CraftActivityProductionNoticeBinding
import com.hopedove.tempdemo.utils.fillGridLayout
import com.hopedove.tempdemo.viewmodel.ProductionNoticeViewModel
import com.journeyapps.barcodescanner.BarcodeEncoder


class MainActivity : AppCompatActivity() {

    lateinit var binding: CraftActivityProductionNoticeBinding
    private val vm: ProductionNoticeViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView(this, R.layout.craft_activity_production_notice)

        vm.productionNoticeDetail.observe(this, { detail ->
            binding.vTable.data = detail
            binding.detail = detail
        })

        vm.knitArrangeBitmap.observe(this, { knitArrangeBitmap ->
            binding.zzplContainer.knitArrangeBitmap = knitArrangeBitmap
        })

        vm.knitArrangeContent.observe(this, { content ->
            binding.zzplContainer.content = content
        })

        vm.triangleArrangeBitmap.observe(this, { bitmap ->
            binding.triangleBitmap = bitmap
        })

        vm.tshtListData.observe(this, { list ->
            binding.tshtListData = list
        })

        vm.dsTableData.observe(this, { list ->
            val (rows, columns) = list[0].dimensionSize
            fillGridLayout(binding.gdTable, rows, columns, list)
        })

        vm.response.observe(this, { resp ->
            binding.resp = resp
        })

        vm.toastText.observe(this, { err ->
            Toast.makeText(this, err, Toast.LENGTH_LONG).show()
        })

        vm.artDesignBitmap.observe(this, { bitmap ->
            binding.ivYjt.setImageBitmap(bitmap)
        })

        vm.noticeBill.observe(this, { bill ->
            binding.containerNoticeBill.item = bill
            bill.CLOTHTKTNO?.let { no ->
                val bitmap = try {
                    val encoder = BarcodeEncoder()
                    val bitmap = encoder.encode(no, BarcodeFormat.CODABAR, 312, 100)
                    encoder.createBitmap(bitmap)
                } catch (e: Exception) {
                    null
                }
                binding.containerNoticeBill.qrCode = bitmap
            }
        })

        vm.loadData(Glide.with(this))
    }


}